<template>
  <div class="register-container">
    <div class="register-form">
      <h2>用户注册</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="username">用户名:</label>
          <input 
            id="username" 
            v-model="registerData.username" 
            type="text" 
            required 
            placeholder="请输入用户名"
          />
        </div>
        
        <div class="form-group">
          <label for="email">邮箱:</label>
          <input 
            id="email" 
            v-model="registerData.email" 
            type="email" 
            required 
            placeholder="请输入邮箱"
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码:</label>
          <input 
            id="password" 
            v-model="registerData.password" 
            type="password" 
            required 
            placeholder="请输入密码"
            minlength="6"
          />
        </div>
        
        <div class="form-group">
          <label for="confirmPassword">确认密码:</label>
          <input 
            id="confirmPassword" 
            v-model="registerData.confirmPassword" 
            type="password" 
            required 
            placeholder="请再次输入密码"
          />
        </div>
        
        <div v-if="error" class="error-message">
          {{ error }}
        </div>
        
        <button type="submit" :disabled="loading">
          {{ loading ? '注册中...' : '注册' }}
        </button>
      </form>
      
      <div class="login-link">
        已有账户？ <router-link to="/login">立即登录</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue';
import { useRouter } from 'vue-router';
import apiService from '../services/ApiService';

const router = useRouter();
const registerData = ref({
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
});
const loading = ref(false);
const error = ref('');

// 监听密码确认字段
watch(() => registerData.value.confirmPassword, (newVal) => {
  if (newVal && registerData.value.password && newVal !== registerData.value.password) {
    error.value = '两次输入的密码不一致';
  } else {
    error.value = '';
  }
});

const handleRegister = async () => {
  try {
    loading.value = true;
    error.value = '';
    
    // 验证密码确认
    if (registerData.value.password !== registerData.value.confirmPassword) {
      error.value = '两次输入的密码不一致';
      loading.value = false;
      return;
    }
    
    const userData = {
      username: registerData.value.username,
      email: registerData.value.email,
      password: registerData.value.password
    };
    
    const response = await apiService.register(userData);
    
    // 修复：检查响应中是否有error属性来判断是否出错
    if (!response || response.error) {
      error.value = response?.error || '注册失败';
    } else {
      // 注册成功，跳转到登录页面
      alert('注册成功，请登录');
      router.push('/login');
    }
  } catch (err) {
    error.value = '网络错误，请稍后重试';
    console.error('注册失败:', err);
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 80vh;
}

.register-form {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.register-form h2 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #333;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: bold;
  color: #555;
}

.form-group input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  box-sizing: border-box;
}

.form-group input:focus {
  outline: none;
  border-color: #4CAF50;
  box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
}

button {
  width: 100%;
  padding: 0.75rem;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:hover:not(:disabled) {
  background-color: #45a049;
}

button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

.error-message {
  color: #f44336;
  background-color: #ffebee;
  padding: 0.75rem;
  border-radius: 4px;
  margin-bottom: 1rem;
  text-align: center;
}

.login-link {
  text-align: center;
  margin-top: 1rem;
  color: #666;
}

.login-link a {
  color: #4CAF50;
  text-decoration: none;
}

.login-link a:hover {
  text-decoration: underline;
}
</style>
```
